package com.servlet;


import com.entity.Product;
import com.service.ProductService;
import com.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加商品
 */
@WebServlet(name = "AddProductServlet",value="/AddProductServlet")
public class AddProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");//从前端获取商品名称
        String type=request.getParameter("type");
        Double price=Double.parseDouble(request.getParameter("price"));//从前端获取商品价格
        Double number=Double.parseDouble(request.getParameter("number"));//从前端获取商品数量
        String guige=request.getParameter("guige");
        String provider=request.getParameter("provider");//从前端获取商品描述
        Product product=new Product(name,type,price,number,guige,provider);
        ProductService productService=new ProductServiceImpl();
        int result=productService.addProduct(product);
        if (result>0){//如果添加商品信息到数据库成功，则显示所有商品
            request.getRequestDispatcher("ShowAllServlet").forward(request,response);
        }
        else {//失败：转到错误提示界面
            request.getRequestDispatcher("fail.jsp").forward(request,response);
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

